﻿using System;
using System.Windows.Forms;
using SPRemoteService.Impl;
using SPRemoteService.WinApp.Properties;

namespace SPRemoteService.WinApp
{
    public partial class ConnectForm : Form
    {
        private MainForm _mainForm;
        //public ConnectForm()
        //{
        //    InitializeComponent();
        //}

        public ConnectForm(MainForm mainForm)
        {
            InitializeComponent();
            _mainForm = mainForm;
        }

        private void ConnectForm_Load(object sender, EventArgs e)
        {
            this.CenterToParent();

            //Load Settings
            txtSiteUrl.Text = (String)Settings.Default.LastSiteUrl;
            txtUserName.Text = (String)Settings.Default.LastUserName;
            txtPassword.Text = (String)Settings.Default.LastPassword;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                SharePointConnector connector = new SharePointConnector();
                connector.Connect(txtUserName.Text,
                    txtPassword.Text,
                    new Uri(txtSiteUrl.Text),
                    "",
                    false
                    );
                _mainForm.Connector = connector;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;

                //Save to Settings
                Settings.Default.LastSiteUrl = txtSiteUrl.Text;
                Settings.Default.LastUserName = txtUserName.Text;
                Settings.Default.LastPassword = txtPassword.Text;
                Settings.Default.Save();
            }
            catch (Exception ex)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.None;
                MessageBox.Show(ex.Message);
            }
        }
    }
}
